package jp.sourceforge.akjrcp.draw;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;



public class DecoratedShapePainter implements Cloneable{
private boolean antialiase=true;
private boolean antialiaseShadow=true;
private boolean fill=true;
private boolean line=true;
private int fillColor=0xffffff;
private int lineColor=0x333333;
//private int lineColor=0;
private int shadowColor=0;

private int lineSize=2;
private int shadowOffsetX=2;
private int shadowOffsetY=2;

private boolean shadow=false;

private Shape selectionShape;

public DecoratedShapePainter clone() throws CloneNotSupportedException{
	return (DecoratedShapePainter) super.clone();
}


private int selectColor=0x008000;
	public BufferedImage createImage(Shape shape){
		int width=shape.getBounds().width+(shadowOffsetX+lineSize)*2;
		int height=shape.getBounds().height+(shadowOffsetY+lineSize)*2;
		BufferedImage bimage=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2=(Graphics2D) bimage.getGraphics();
		g2.translate(shadowOffsetX+lineSize, shadowOffsetY+lineSize);
		paint(g2,shape);
		return bimage;
	}
	
	public void paint(Graphics2D g2,Shape shape){
		g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
	             RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_RENDERING,
	             RenderingHints.VALUE_RENDER_QUALITY);
		
		
		
		//what is this?
		//Rectangle rectangle=shape.getBounds();
		//int offsetX=rectangle.x;
		//int offsetY=0;//-rectangle.y;
		
		//rectangle.translate(offsetX,offsetY);
		
		if(shadow){
			if(antialiaseShadow){
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			             RenderingHints.VALUE_ANTIALIAS_ON);
			}else{
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			             RenderingHints.VALUE_ANTIALIAS_OFF);
			}
			g2.translate(shadowOffsetX,shadowOffsetY);
			g2.setStroke(new BasicStroke(lineSize));
			g2.setColor(new Color(shadowColor));
			
			g2.draw(shape);
			g2.fill(shape);
			g2.translate(-shadowOffsetX,-shadowOffsetY);
		}
		
		if(antialiase){
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		             RenderingHints.VALUE_ANTIALIAS_ON);
		}else{
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		             RenderingHints.VALUE_ANTIALIAS_OFF);
		}
		//draw main
		if(line){
		g2.setStroke(new BasicStroke(lineSize));
		g2.setColor(new Color(lineColor));
		g2.draw(shape);
		}
		
		if(fill){
		
		g2.setColor(new Color(fillColor));
		g2.fill(shape);		
		}
		
		if(selectionShape!=null){
			Graphics2D tmpG=(Graphics2D) g2.create();
			tmpG.clip(selectionShape);
			tmpG.setColor(new Color(selectColor));
			//log.trace("shaped: selection="+selectionShape.getBounds()+"; shape="+shape.getBounds());
			tmpG.fill(shape);
		}
		
	}

	public boolean isAntialiase() {
		return antialiase;
	}

	public void setAntialiase(boolean antialiase) {
		this.antialiase = antialiase;
	}

	public boolean isAntialiaseShadow() {
		return antialiaseShadow;
	}

	public void setAntialiaseShadow(boolean antialiaseShadow) {
		this.antialiaseShadow = antialiaseShadow;
	}

	public boolean isFill() {
		return fill;
	}

	public void setFill(boolean fill) {
		this.fill = fill;
	}

	public int getFillColor() {
		return fillColor;
	}

	public void setFillColor(int fillColor) {
		this.fillColor = fillColor;
	}

	public boolean isLine() {
		return line;
	}

	public void setLine(boolean line) {
		this.line = line;
	}

	public int getLineColor() {
		return lineColor;
	}

	public void setLineColor(int lineColor) {
		this.lineColor = lineColor;
	}

	public int getLineSize() {
		return lineSize;
	}

	public void setLineSize(int lineSize) {
		this.lineSize = lineSize;
	}

	public boolean isShadow() {
		return shadow;
	}

	public void setShadow(boolean shadow) {
		this.shadow = shadow;
	}

	public int getShadowColor() {
		return shadowColor;
	}

	public void setShadowColor(int shadowColor) {
		this.shadowColor = shadowColor;
	}

	public int getShadowOffsetX() {
		return shadowOffsetX;
	}

	public void setShadowOffsetX(int shadowOffsetX) {
		this.shadowOffsetX = shadowOffsetX;
	}

	public int getShadowOffsetY() {
		return shadowOffsetY;
	}

	public void setShadowOffsetY(int shadowOffsetY) {
		this.shadowOffsetY = shadowOffsetY;
	}

	public int getSelectColor() {
		return selectColor;
	}

	public void setSelectColor(int selectColor) {
		this.selectColor = selectColor;
	}

	public Shape getSelectionShape() {
		return selectionShape;
	}

	public void setSelectionShape(Shape selectionShape) {
		this.selectionShape = selectionShape;
	}


}
